import {
  Movie
} from 'class/Movie.js';
var app = getApp();
Page({
  data: {
    start: 0,
    count: 20,
    comments: [],
    movie: {}
  },
  onLoad: function(options) {
    var movieId = options.id;

    this.getMovieData(movieId);
    this.getMovieCommentsData(movieId);
  },

  getMovieCommentsData: function(movieId) {
    var that = this;
    var start = this.data.start;
    var count = this.data.count;
    var url = app.globalData.doubanBase +
      "/v2/movie/subject/" + movieId + "/comments?apikey=" + app.globalData.doubanApiKey + "&start=" + start + "&count=" + count;

    wx.request({
      url: url,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "json"
      },
      success: function(res) {
        if (res.statusCode == 200) {
          that.setData({
            comments: res.data.comments,
          });
        }
      },
      fail: function(error) {
        // fail
        console.log(error)
      }
    })
  },
  getMovieData: function(movieId) {
    var that = this;
    var url = app.globalData.doubanBase +
      "/v2/movie/subject/" + movieId + "?apikey=" + app.globalData.doubanApiKey;
    var movie = new Movie(url);
    // var movieData = movie.getMovieData();
    // var that = this;
    // movie.getMovieData(function (movie) {
    //   that.setData({
    //     movie: movie
    //   })
    // })
    //C#、Java、Python lambda
    movie.getMovieData((movie) => {
      this.setData({
        movie: movie
      })
    })
  },

  /*查看图片*/
  viewMoviePostImg: function(e) {
    var src = e.currentTarget.dataset.src;
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: [src] // 需要预览的图片http链接列表
    })
  },
})